< previous page page_340 next page >

Page 340
        ' Make dx = 1 pixel.
        dx = picCanvas.ScaleX(1, vbPixels, _
            picCanvas.ScaleMode) 
             
        X = x1 + dx 
        Do While X < x2 
            picCanvas.Line -(X, PolynomialValue(a_values, X)) 
            X = X + dx 
        Loop 

        picCanvas.Line -(x2, PolynomialValue(a_values, x2)) 
    Else
        ' There is no solution. 
        MsgBox "There is no solution." 
    End If 
End Sub 

' Compute the a, b, and c values for quadratic least squares.
' Return True if there is a solution.
Private Function GetLeastSquaresValues( _
    ByVal degree As Integer, X() As Single, Y() As Single, _
    a_values() As Single) As Boolean
Dim max_point As Integer
Dim coeff() As Single
Dim row As Integer
Dim col As Integer
Dim i As Integer
Dim total As Single

    max_point = UBound(X) - 1
     
    ' Find the coefficients for the equations.
    ReDim coeff(0 To degree, 0 To degree + 1)
    For row = 0 To degree
        ' Find the coefficients for the columns.
        For col = 0 To degree
            ' Find Sum(Xi^(row + col)) over all i.
            total = 0
            For i = 0 To max_point
                total = total + X(i + 1) ^ (row + col)
            Next i
            coeff(row, col) = total
        Next col

        ' Find the constant term.
        total = 0
        For i = 0 To max_point
            total = total + Y(i + 1) * X(i + 1) ^ row
        Next i
        coeff(row, degree + 1) = total
    Next row

 
< previous page page_340 next page >